package com.yoomiito.app.ui.anyview.college

import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import cn.droidlover.xdroidmvp.cache.SharedPref
import cn.jzvd.JzvdStd
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.model.colleage.SubjectPlayInfo
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.videoplayer.VideoPlayer
import kotlinx.android.synthetic.main.act_subject_detail.*

class VideoLayout(val context: Context, val frameLayout: FrameLayout){
    private lateinit var iv:ImageView
    private lateinit var videoPlayer:VideoPlayer
    private var tipTv:TextView?=null
    private var time = 0
    private var lastTime = 0
    var progress:(Int)->Unit={}
    var videoState:(Int)->Unit={}

    fun showPic(){
        frameLayout.removeAllViews()
        iv = ImageView(context)
        val ivLayoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,DensityUtil.dp2px(171f))
        frameLayout.addView(iv,ivLayoutParams)
    }

    fun setPicUrl(url:String){
        ImageLoader.getInstance().into(context,url,iv)
    }

    fun showVideo(){
        frameLayout.removeAllViews()
        val lp = frameLayout.layoutParams
        lp.height = App.mWidthPx * 9 / 16

        videoPlayer = VideoPlayer(context)
        videoPlayer.playStateListener = {state,value->
            videoState(state)
            when(state){
                VideoPlayer.START_PLAY-> tipTv?.visibility = View.GONE
                VideoPlayer.PLAY_PROGRESS->{
                    //进度
                    if (value-lastTime>1 || lastTime-value>1){//快进

                    }else{
                        time+=(value-lastTime)
                        progress(20-time)
                    }
                    lastTime = value

                }
            }
        }
//        videoPlayer.setUp("http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4",
//                "", JzvdStd.SCREEN_NORMAL, JZMediaIjk::class.java)
        videoPlayer.thumbImageView.alpha = 0.3f
//        ImageLoader.getInstance().into(context,"http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640",videoPlayer.thumbImageView)
        val videoLayoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT)
        frameLayout.addView(videoPlayer,videoLayoutParams)
        if (UserInfoConfig.getUser().course_status==0){
            tipTv = TextView(context)
            tipTv?.text = "学习视频20秒后就能领取福利啦！"
            tipTv?.setTextColor(ResUtil.getColor(R.color.color_orange))
            tipTv?.textSize = 14f
            tipTv?.visibility = View.GONE
            val tipLayoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT)
            tipLayoutParams.gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
            tipLayoutParams.bottomMargin = (App.mWidthPx*9/16*0.3).toInt()
            frameLayout.addView(tipTv,tipLayoutParams)
        }
    }

    fun setVideoSource(videoUrl:String,thumbUrl:String,autoPlay:Boolean){
//        videoPlayer.setUp("http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4",
//                "", JzvdStd.SCREEN_NORMAL, JZMediaIjk::class.java)
        videoPlayer.setUp(videoUrl, "",JzvdStd.SCREEN_NORMAL)//, JZMediaIjk::class.java
        ImageLoader.getInstance().into(context,thumbUrl,videoPlayer.thumbImageView)
        if (autoPlay)videoPlayer.startButton.performClick()
    }

    fun showTip(show:Boolean){
        tipTv?.visibility = if (show) View.VISIBLE else View.GONE
    }
}